package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.common.Common;
import com.common.EncryptUtils;
import com.common.PageUtils;
import com.common.ReturnCode;
import com.common.TokenUtils;
import com.dao.SysOperateLogDao;
import com.dao.SysUserDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.ReponseDto;
import com.model.SysMenu;
import com.model.SysOperateLog;
import com.model.SysUser;
import com.service.ISysUserService;

@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysOperateLogDao sysOperateLogDao;

    @Override
    public ReponseDto<String> login(String userName, String password) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("username", userName);
        parMap.put("password", EncryptUtils.encryptPassword(password));
        List<SysUser> ulist = sysUserDao.get(parMap);
        if (CollectionUtils.isEmpty(ulist)) {
            return new ReponseDto<>(ReturnCode.LOGIN_ERROR);
        }
        String token = tokenUtils.generateToken(ulist.get(0));
        return new ReponseDto<>(token);
    }

    @Override
    public ReponseDto<List<SysMenu>> menu(String id) {
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("userId", id);
        return new ReponseDto<>(menuTotree(sysUserDao.getmenu(parMap)));
    }

    @Override
    public ReponseDto<List<SysMenu>> getMenuByRoleId(Integer roleId) {
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("roleId", roleId);
        return new ReponseDto<>(menuTotree(sysUserDao.getmenu(parMap)));
    }

    private List<SysMenu> menuTotree(List<SysMenu> slist) {
        List<SysMenu> rootlist = new ArrayList<>();
        if (CollectionUtils.isEmpty(slist)) {
            return slist;
        }
        for (SysMenu sm : slist) {
            if (sm.getPid() == 0) {
                rootlist.add(sm);
            }
        }
        for (SysMenu rl : rootlist) {
            getMenuChild(slist, rl);
        }
        return rootlist;
    }

    private void getMenuChild(List<SysMenu> slist, SysMenu root) {
        List<SysMenu> chdlist = new ArrayList<>();
        for (SysMenu s : slist) {
            if (s.getPid() == root.getId()) {
                chdlist.add(s);
            }
        }
        if (!CollectionUtils.isEmpty(chdlist)) {
            root.setChdlist(chdlist);
            for (SysMenu chd : chdlist) {
                getMenuChild(slist, chd);
            }
        }

    }

    @Override
    public ReponseDto<PageInfo> list(String name, Integer roleId,String pageNo,Integer pageSize) {
        if(null == pageSize) {
            pageSize = PageUtils.pageSize;
        }
        Page page = PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("name", name);
        parMap.put("roleId", roleId);
        sysUserDao.get(parMap);
        PageInfo info = new PageInfo<>(page.getResult());
        return new ReponseDto<>(info);
    }

    @Override
    public ReponseDto<String> edit(SysUser sysuser, String userId, String userName) {
        if (null == sysuser.getId() || StringUtils.isEmpty(sysuser.getName())
                || StringUtils.isEmpty(sysuser.getPassword()) || StringUtils.isEmpty(sysuser.getPhone())
                || sysuser.getRoleId() == null || StringUtils.isEmpty(sysuser.getRemarks())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }

        // 设置密码
        sysuser.setPassword(EncryptUtils.encryptPassword(sysuser.getPassword()));
        sysUserDao.edit(sysuser);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
        sysOperateLog.setDescription("系统管理员-编辑用户");
        sysOperateLog.setDataCcope("系统管理员-编辑用户");
        sysOperateLogDao.insert(sysOperateLog);

        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<String> delete(Integer id, String userId, String userName) {
        if (null == id) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        sysUserDao.delete(id);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("系统管理员-删除用户");
        sysOperateLog.setDataCcope("系统管理员-删除用户");
        sysOperateLogDao.insert(sysOperateLog);

        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<List<SysMenu>> getMenuByRoleIdAll(Integer roleId) {

        Map<String, Object> parMap = new HashMap<>();
        parMap.put("roleId", roleId);
        return new ReponseDto<>(sysUserDao.getmenuAll(parMap));
    }

}
